package com.example.bijidemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bijidemo.common.R;
import com.example.bijidemo.common.SysLog;
import com.example.bijidemo.entity.Employee;
import com.example.bijidemo.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @DeleteMapping("/{id}")
    @SysLog("删除一个员工")
    public R<String>  deletes(@PathVariable Long id){
            employeeService.removeById(id);
            return R.success("删除成功");
    }


    @PostMapping("/login")
    @SysLog("进行登录的请求")
    public R<Employee> login(HttpServletRequest httpServletRequest, @RequestBody Employee employee){
        //1.先把密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据前端传过来的username查询密码
        LambdaQueryWrapper<Employee> QueryWrapper=new LambdaQueryWrapper<>();
        QueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(QueryWrapper);
        //3.如果没有查询到则返回登录失败
        if (emp==null){
            return R.error("登录失败");
        }
        //4.密码不对则返回登录失败
        if (!emp.getPassword().equals(password)){
            return R.error("密码不对登录失败");
        }
        //5.查看员工如果是已经禁用则显示该账号已经禁用
        if (emp.getStatus()==0){
            return R.error("该员工已经被禁用");

        }
        //6.登录成功将id放入session并且 返回登录成功跳转界面
        httpServletRequest.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    @SysLog("进行退出登录的请求")
    public R<String> logout(HttpServletRequest request){
        //清理session中保存的员工id;
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    @PostMapping
    @SysLog("新增人员的操作")
    public R<String> save(HttpServletRequest request, HttpServletResponse response,@RequestBody Employee employee){
        log.info("新增员工员工的信息为{}",employee.toString());
        //        给用户设置初始密码并且md5加密
        String password ="123456";
        employee.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        log.info(" 封装好的员工信息{}",employee);
        //利用mybatispuls新增一个员工
        employeeService.save(employee);

        return R.success("新增成功");

    }

    @GetMapping("/page")
    @SysLog("员工的分页查询")
    //编写分页查询的功能
    public R<Page> page(int page,int pageSize,String name){
        log.info("page={},pagesize={},name={}",page,pageSize,name);
        //获取分页构造器
        Page pageInfo=new Page(page,pageSize);
        //获取查询构造器
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //条件过滤
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序过滤
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //具体查询
         employeeService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);




    }


    @PutMapping
    @SysLog("修改员工的操作")
    public R<String> update(@RequestBody Employee employee){

//        进行对员工信息的更改
        boolean b = employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    //根据id查询信息进行回显
    @GetMapping("{id}")
    @SysLog("根据id跳转到员工修改界面的操作")
    public R<Employee> bianji(@PathVariable Long id){
        log.info("根据id查询信息");
        //根据id查询数据库 并且把信息封装成表给前端
        Employee byId = employeeService.getById(id);
        if (byId==null){
            return R.error("没有查询到该员工的信息");
        }
        return R.success(byId);

    }

    //根据下拉框查询指派人  所有的员工
    @GetMapping("/list")
    public R<List<Employee>> getZhipai(){
        List<Employee> list = employeeService.list();
        return R.success(list);
    }




}

